package com.songhuan.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songhuan.web.dto.*;
import com.songhuan.web.dto.query.*;
import com.songhuan.web.entity.*;
import com.songhuan.web.mapper.*;
import com.songhuan.web.service.*;
import com.songhuan.web.tools.dto.*;
import com.songhuan.web.dto.AppUserDto;
import com.songhuan.web.dto.UserFaceDto;
import com.songhuan.web.dto.query.UserFacePagedInput;
import com.songhuan.web.entity.AppUser;
import com.songhuan.web.entity.UserFace;
import com.songhuan.web.mapper.AppUserMapper;
import com.songhuan.web.mapper.UserFaceMapper;
import com.songhuan.web.service.UserFaceService;
import com.songhuan.web.tools.Extension;
import com.songhuan.web.tools.dto.PagedResult;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import lombok.SneakyThrows;
import com.songhuan.web.tools.*;
/**
 * 用户人脸功能实现类
 */
@Service
public class UserFaceServiceImpl extends ServiceImpl<UserFaceMapper, UserFace> implements UserFaceService {

    /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的UserFace表mapper对象
     */
    @Autowired
    private UserFaceMapper _UserFaceMpper;




    /**
     * 用户人脸分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<UserFaceDto> List(UserFacePagedInput input) {


        //声明一个支持用户人脸查询的(拉姆达)表达式
        LambdaQueryWrapper<UserFace> queryWrapper = Wrappers.<UserFace>lambdaQuery()
                .eq(input.getId()!=null&&input.getId()!=0,UserFace::getId,input.getId())
                .eq(input.getCreatorId()!=null,UserFace::getCreatorId,input.getCreatorId());
        //如果前端搜索传入查询条件则拼接查询条件

        if(Extension.isNotNullOrEmpty(input.getFaceToken())) {
            queryWrapper=queryWrapper.eq(UserFace::getFaceToken,input.getFaceToken());
        }
        if(input.getUserId()!=null) {
            queryWrapper=queryWrapper.eq(UserFace::getUserId,input.getUserId());
        }

        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(UserFace::getCreationTime);
        //构建一个分页查询的model
        Page<UserFace> page = new Page<>(input.getPage(), input.getLimit());


        //从数据库进行分页查询获取用户人脸数据
        IPage<UserFace> pageRecords= _UserFaceMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _UserFaceMpper.selectCount(queryWrapper);

        //把UserFace实体转换成UserFace传输模型
        List<UserFaceDto> items= Extension.copyBeanList(pageRecords.getRecords(),UserFaceDto.class);
        for (UserFaceDto item : items) {

            //查询出关联的创建用户信息
            AppUserDto CreatorAppUserDTO=new AppUserDto();
            AppUser CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
            if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }



            //查询出关联的AppUser表信息
            AppUserDto UserDTO = new AppUserDto();
            AppUser  UserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getUserId()));
            if(UserEntity!=null) {
                BeanUtils.copyProperties(UserDTO,UserEntity);
                item.setUserDto(UserDTO);
            }

        }

        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }

    /**
     * 单个用户人脸查询
     */
    @SneakyThrows
    @Override
    public UserFaceDto Get(UserFacePagedInput input) {
        if(input.getId()==null)
        {
            return new UserFaceDto();
        }
        PagedResult<UserFaceDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new UserFaceDto();
    }

    /**
     *用户人脸创建或者修改
     */
    @SneakyThrows
    @Override
    public UserFaceDto CreateOrEdit(UserFaceDto input) {

        //声明一个用户人脸实体
        UserFace UserFace = new UserFace();

        //把前端传入的input参数拷贝到用户人脸实体
        BeanUtils.copyProperties(UserFace,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(UserFace);

        //定义一个返回给前端的用户人脸传输模型
        UserFaceDto UserFaceDto=new UserFaceDto();

        //同理把操作的用户人脸实体拷贝给用户人脸传输模型
        BeanUtils.copyProperties(UserFaceDto,UserFace);

        //把传输模型返回给前端
        return UserFaceDto;
    }



}
